<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
        <title>MINI-TextField</title>  
        <script src="https://asmstatic.jsonmedia.com/js/boot.js" type="text/javascript"></script>
        <script type="text/javascript" src="../../dialogs/internal.js"></script>   
        <script type="text/javascript" src="../js/control.js"></script>
        <link href="../css/commonTable.css" rel="stylesheet" type="text/css">   
</head> 
<body>  
    <form id="miniForm">
     <div id=nr_add></div>
    </form>  
    <script type="text/javascript">
    var form=""; 
    
    /**编辑的控件的值*/
    var oNode   = null,  
    thePlugins  = '';
    var allData ='';
    var att_array = [];
	var event_array = [];
	var database_array = [];
    var controlId ="";
    var htmlName ="";
    $(document).ready(function(){
    	controlId  = getQueryString("controlId");
    	var projectName = getQueryString("projectName");
    	htmlName = getQueryString("htmlName");
    	thePlugins = htmlName;
    	queryAllControlWidgetEntity(projectName,controlId,setAllAttr);
    	
    	});
        
    function setAllAttr(data){
    	var json = mini.decode(data);
    	for(var i =0 ;i<json.length;i++){
    		if(json[i].attrTypeId=="1"){
    			att_array.push(json[i]);
    		}else if(json[i].attrTypeId=="3"){
    			database_array.push(json[i])
    		}else{
    			event_array.push(json[i]);
    		}
    	}
    	initTable(database_array,"控件数据库配置");
    	initTable(att_array,"控件属性");
    	initTable(event_array,"控件事件");
    	 //若控件已经存在，则设置回调其值  
        if( UE.plugins[thePlugins].editdom ){  
            oNode = UE.plugins[thePlugins].editdom;  
            mini.parse();  
         	form = new mini.Form('miniForm')
            callbackValue(oNode); 
         }else{
        	mini.parse();  
         	form = new mini.Form('miniForm')
         }
       
    }
    
    function createElement(type, name){       
            var element = null;       
            try {          
                element = document.createElement('<'+type+' id="'+name+'">');       
            } catch (e) {}     
            if(element==null) {       
                element = document.createElement(type);       
                element.name = name;       
            }   
            return element;       
        }  
         //取消按钮  
        dialog.oncancel = function () {  
            if( UE.plugins[thePlugins].editdom ) {  
                delete UE.plugins[thePlugins].editdom;  
            }  
        };  
        //确认  
        dialog.onok = function (){  
            form.validate();  
            if (form.isValid() == false) {  
                return false;  
            }  
            //控件尚未存在，则创建新的控件，否则进行更新  
            if( !oNode ) {  
                try {  
                    oNode = initoNode(att_array,event_array,database_array);
                    //需要设置该属性，否则没有办法其编辑及删除的弹出菜单  
                    if(oNode.getAttribute("prefix") && oNode.getAttribute("prefix").trim()!="" ){
                    	  oNode.setAttribute('plugins',thePlugins);  
                          editor.execCommand('insertHtml',oNode.outerHTML);
                    }else{
                    	  mini.alert('号码前缀必填'); 
                    }
                } catch (e) {  
                    try {  
                        editor.execCommand('error'); 
                        mini.alert('控件异常，请联系技术支持'); 
                    } catch ( e ) {  
                        mini.alert('控件异常，请联系技术支持');  
                    }  
                    return false;  
                }  
            } else {//已经存在，则更新属性则可  
            	oNode = update(oNode,att_array,event_array,database_array);
            	oNode.setAttribute('plugins',thePlugins); 
                delete UE.plugins[thePlugins].editdom;  
            }  
        };  
          function callbackValue(oNode){
        	  update_back(oNode,att_array,event_array,database_array);
        	  oNode.setAttribute('plugins',thePlugins); 
           }
        function initTable(tds_array,titleName){
          var regAttrValue =new RegExp("@@attrValue@@","g");
          var regAttrKey =new RegExp("@@attrKey@@","g");
   		  var att_temp_start = '<fieldset style="border: solid 1px #aaa; padding: 8px;"><legend>'+titleName+'</legend><table class="table-detail commonTable"  border="1" width="100%" bordercolor="#e5e5e5">'; 
   		  var temp_end ="</table></fieldset>";
   		  var mod_1_temp = '<tr><td>@@attrValue@@</td><td><input id="@@attrKey@@" class="mini-textbox" ></td>';
   		  var mod_2_temp ='<td>@@attrValue@@</td><td><input id="@@attrKey@@" class="mini-textbox" ></td></tr>';
   		  var mod_2_end ='<td></td><td></td></tr>';
   		  var out="";
   		  var tds_array_mod = (tds_array.length%2==0);
   		  for(var i=1;i<tds_array.length+1;i++){
   			 if(i==1){
   				out = out+att_temp_start;
   			 }
   			 if(i%2==0){
   	   				out= out+mod_2_temp.replace(regAttrValue,tds_array[i-1].attrValue+":").replace(regAttrKey,tds_array[i-1].attrKey); 
   				
   			 }else{
   					out= out+mod_1_temp.replace(regAttrValue,tds_array[i-1].attrValue+":").replace(regAttrKey,tds_array[i-1].attrKey);
   			 }
   			if((!tds_array_mod) && i==tds_array.length){
   				out= out+mod_2_end;
   			}
   		  }
   		   out= out+temp_end;
   		$("#nr_add").append(out);
        }  
        function initoNode(att_array,event_array,database_array){
        	var oNodeoNode
             for(var i=0;i<att_array.length;i++){
            	 if(att_array[i].attrKey=='id'){
            		 oNode = createElement('input',att_array[i].attrKey);  
                     oNode.setAttribute('class',htmlName+" mini-textbox");
            		 oNode.setAttribute('value',"===ID选择器===");
            		 oNode.setAttribute('readonly',"readonly");
            	 }
            	 if(mini.get(att_array[i].attrKey).getValue()!=""){
            	 	 oNode.setAttribute(att_array[i].attrKey,mini.get(att_array[i].attrKey).getValue()); 
            	 }else{
   	      	    	oNode.removeAttribute(att_array[i].attrKey);
   	      	    }
              }
        	for(var i=0;i<database_array.length;i++){
           	  if(mini.get(database_array[i].attrKey).getValue()!=""){
           	 	 oNode.setAttribute(database_array[i].attrKey,mini.get(database_array[i].attrKey).getValue()); 
           	 }else{
  	      	    	oNode.removeAttribute(database_array[i].attrKey);
  	      	    }
             }
             for(var i=0;i<event_array.length;i++){
            	 if(mini.get(event_array[i].attrKey).getValue()!=""){
            		 oNode.setAttribute(event_array[i].attrKey,mini.get(event_array[i].attrKey).getValue());
                 }else{
   	      	    	oNode.removeAttribute(event_array[i].attrKey);
   	      	    }
            }
             return oNode;
        }  
        
        function update(oNode,att_array,event_array,database_array){
        	  for(var i=0;i<att_array.length;i++){
        		  var newValue =  mini.get(att_array[i].attrKey).getValue();
        		  if(newValue!=""){
        			oNode.setAttribute(att_array[i].attrKey,mini.get(att_array[i].attrKey).getValue());
        		  }else{
  	      	    	oNode.removeAttribute(att_array[i].attrKey);
  	      	    	}
        		}
              for(var i=0;i<event_array.length;i++){
            	 if(mini.get(event_array[i].attrKey).getValue()!=""){
            		 oNode.setAttribute(event_array[i].attrKey,mini.get(event_array[i].attrKey).getValue());
                  }else{
  	      	    	oNode.removeAttribute(event_array[i].attrKey);
  	      	    }
            }
              for(var i=0;i<database_array.length;i++){
            	  var newValue =  mini.get(database_array[i].attrKey).getValue();
        		  var oldValue =  oNode.getAttribute(database_array[i].attrKey);
             	 if(newValue!=""){
             		if(database_array[i].attrKey=="columnName"){
             			if(newValue!=oldValue){
							oNode.setAttribute("oldcolumnName",oldValue);
             			}
        			 }
             		 oNode.setAttribute(database_array[i].attrKey,mini.get(database_array[i].attrKey).getValue());
                   }else{
   	      	    	oNode.removeAttribute(database_array[i].attrKey);
   	      	    }
             }
              return oNode;
        }
        function update_back(oNode,att_array,event_array,database_array){
      	  for(var i=0;i<att_array.length;i++){
	      		if(oNode.getAttribute(att_array[i].attrKey)!=""){
	      			mini.get(att_array[i].attrKey).setValue(oNode.getAttribute(att_array[i].attrKey));
	      	    }else{
	      	    	oNode.removeAttribute(att_array[i].attrKey);
	      	    }
      		}
            for(var i=0;i<event_array.length;i++){
            	if(oNode.getAttribute(event_array[i].attrKey)!=""){
            	 mini.get(event_array[i].attrKey).setValue(oNode.getAttribute(event_array[i].attrKey));
            	}else  {
	      	    	oNode.removeAttribute(event_array[i].attrKey);
	      	    }
             }
            for(var i=0;i<database_array.length;i++){
            	if(oNode.getAttribute(database_array[i].attrKey)!=""){
            	 mini.get(database_array[i].attrKey).setValue(oNode.getAttribute(database_array[i].attrKey));
            	}else  {
	      	    	oNode.removeAttribute(database_array[i].attrKey);
	      	    }
             }
            return oNode;
      }
    </script>  
</body>  
</html>  